package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.domain.Area;
import com.example.mybatisplus.mapper.AreaMapper;
import com.example.mybatisplus.model.domain.Manager;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.AreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2024-07-17
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {
    @Autowired
    private AreaMapper areaMapper;
        @Override
        public Page<Area> pageList(Area area, PageDTO dto) {
            QueryWrapper<Area> wrapper =new QueryWrapper<>();
            if(Objects.nonNull(area.getAreaId())){
                wrapper.eq("areaId",area.getAreaId());
            }
            Page<Area> page = new Page<>();
            page.setCurrent(dto.getPageNo()).setSize(dto.getPageSize());
            baseMapper.selectPage(page,wrapper);
            return page;
        }

    @Override
    public boolean removeArea(Area area) {
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.eq("areaId",area.getAreaId());
        Area one = baseMapper.selectOne(wrapper);
        if(one.getBuildingNum() == 0 && one.getRoomNum() == 0 && one.getBedNum() == 0){ // 该园区可被删除
            baseMapper.deleteById(one.getAreaId());
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean forceRemoveArea(Area area) {
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.eq("areaId",area.getAreaId());
        Area one = baseMapper.selectOne(wrapper);
        one.setBuildingNum(0);
        one.setRoomNum(0);
        one.setBedNum(0);
        baseMapper.updateById(one);
        baseMapper.deleteById(one.getAreaId());
        return true;
    }
    @Override
    public List<String> getAllAreaNames() {
        return areaMapper.getAllAreaNames();
    }

}

